package com.quartz.jobTask;

import com.quartz.config.JobConstant;
import com.utility.util.DateUtils;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;

import java.util.Objects;

/**
 * @author
 */
public class TriggerInstantiation extends JobDetailInstantiation {

    @Override
    public Trigger messageStorage(MessageInfo messageInfo) {
        /**
         *  创建触发期实例
         */
        TriggerBuilder build = TriggerBuilder.newTrigger().withIdentity(new TriggerKey(messageInfo.getApplicationCode() + "_" + messageInfo.getTaskId() +"_"+ JobConstant.JOB
                , messageInfo.getApplicationCode()))
                .startNow();
        // 执行策略
        if (!Objects.isNull(messageInfo.getScheduleBuilder())) {
            build.withSchedule(messageInfo.getScheduleBuilder());
        }
        if (Objects.isNull(messageInfo.getStartDate()) && Objects.isNull(messageInfo.getEndDate())){
            build.startNow();
        }
        if (!Objects.isNull(messageInfo.getStartDate())){
            build.startAt(DateUtils.transitionDate(messageInfo.getStartDate()));
        }
        if (!Objects.isNull(messageInfo.getEndDate())){
            build.endAt(DateUtils.transitionDate(messageInfo.getEndDate()));
        }
        return (SimpleTrigger) build;
    }


}
